package com.compass.mobile.ui.dialog

import AppManager
import android.app.Dialog
import android.os.Build
import android.text.Html
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import androidx.annotation.ColorInt
import androidx.fragment.app.FragmentActivity
import com.compass.mobile.databinding.DialogAgreementMessageBinding
import com.compass.mobile.framework.base.BaseDialog
import com.compass.mobile.framework.base.BaseDialog.AnimStyle.DEFAULT
import com.compass.mobile.framework.base.BaseDialogFragment
import com.compass.mobile.framework.ext.onClick
import com.compass.mobile.framework.utils.FocusViewUtils

/**
 * Description： 协议弹窗
 * Date:2024/11/12 10:12
 */
class AgreementDialog  {

    class Builder(
        activity: FragmentActivity,agreementType:Int,htmlData:String?
    ) : BaseDialogFragment.Builder<Builder>(activity) {
        private val mBinding: DialogAgreementMessageBinding =
            DialogAgreementMessageBinding.inflate(LayoutInflater.from(activity))

        private var mAutoDismiss = true // 设置点击按钮后自动消失

        init {
            setContentView(mBinding.root)
            setWidth((AppManager.getScreenHeightPx() * 0.8).toInt())
            setHeight((AppManager.getScreenWidthPx() * 0.8).toInt())
            setAnimStyle(DEFAULT)
            setGravity(Gravity.CENTER)
            FocusViewUtils.scaleView(mBinding?.tvDialogMessageCancel, 1)
            FocusViewUtils.scaleView(mBinding?.tvDialogMessageConfirm, 1)
            when(agreementType){
                0->{
                    setTitle("用户协议")

                }
                1->{
                    setTitle("隐私协议")
                }
                2->{
                    setTitle("问诊须知")
                }
                3->{
                    setTitle("护理须知")
                }
                4->{
                    setTitle("自主开单须知")
                }
                5->{
                    setTitle("预约挂号须知")
                }
            }
            htmlData?.let {
                setMessage(htmlData)
            }


        }

        fun setTitle(resId: Int): Builder {
            return setTitle(getText(resId))
        }

        fun setTitle(text: CharSequence?): Builder {
            mBinding.tvDialogMessageTitle.text = text
            return this
        }


        fun setMessage(text: String): Builder {
            //设置默认为utf-8
            mBinding.tvDialogMessageMessage.getSettings().defaultTextEncodingName = "UTF -8";
//主要是下面这行代码和这个构造方法
            mBinding.tvDialogMessageMessage.loadData(text, "text/html; charset=UTF-8", null)

            return this
        }



        fun setCancel(resId: Int): Builder {
            return setCancel(getText(resId))
        }

        fun setCancel(text: CharSequence?): Builder {
            mBinding.tvDialogMessageCancel.text = text
            val isEmpty = text.isNullOrEmpty()
            mBinding.tvDialogMessageCancel.visibility = if (isEmpty) View.GONE else View.VISIBLE
//            mBinding.vDialogMessageLine.visibility = if (isEmpty) View.GONE else View.VISIBLE
//            mBinding.tvDialogMessageConfirm.setBackgroundResource(if (isEmpty) R.drawable.dialog_message_one_button else R.drawable.dialog_message_right_button)
            return this
        }

        fun setConfirm(resId: Int): Builder {
            return setConfirm(getText(resId))
        }

        fun setConfirm(text: CharSequence?): Builder {
            mBinding.tvDialogMessageConfirm.text = text
            return this
        }

        fun setConfirmTxtColor(@ColorInt color: Int): Builder {
            mBinding.tvDialogMessageConfirm.setTextColor(color)
            return this
        }

        fun setCancelTextColor(@ColorInt color: Int): Builder {
            mBinding.tvDialogMessageCancel.setTextColor(color)
            return this
        }

        fun setAutoDismiss(dismiss: Boolean): Builder {
            mAutoDismiss = dismiss
            return this
        }

        fun setonConfirmListener(onConfirm: (dialog: Dialog?) -> Unit): Builder {
            mBinding.tvDialogMessageConfirm.onClick {
                onConfirm?.invoke(dialog)
            }
            return this
        }

        fun setonCancelListener(onCancel: (dialog: Dialog?) -> Unit): Builder {
            mBinding.tvDialogMessageCancel.onClick {
                onCancel?.invoke(dialog)
            }
            return this
        }

        override fun create(): BaseDialog {
            // 如果标题为空就隐藏
            if (mBinding.tvDialogMessageTitle.text.isNullOrEmpty()) {
                mBinding.tvDialogMessageTitle.visibility = View.GONE
            }
            // 如果内容为空就抛出异常
//           if (TextUtils.isEmpty(mMessageView.getText())) {
//                //throw new IllegalArgumentException("Dialog message not null");
//           }
            return super.create()
        }
    }
}